﻿/**
* userCtrl - AngularJS module for right sidebar.
*
*
* Credits
* =======
*
* Copyright 2015 LonghronShen <LonghronShen@Hotmail.com>
*/


(function () {
    "use strict";

    /**
     * Config
     */
    var moduleName = 'app';
    var DEFAULT_ID = '__default';

    /**
     * Module
     */
    var module;
    try {
        module = angular.module(moduleName);
    } catch (err) {
        // named module does not exist, so create one
        module = angular.module(moduleName, []);
    }

    module.controller('carrierOrderCtrl', ['$scope', '$ngBootbox', '$stateParams', 'dataService', '$state', function ($scope, $ngBootbox, $stateParams, dataService, $state) {
        dataService.GetMyOrderList(0, 2).then(function (res) {
            console.info('res getMyOrderList:', res);
        }, function (rej) { console.info('rej:', rej); })
        //$scope.searchingState = "";
        //$scope.searchingStartingPlaceCode = '';
        //$scope.searchingDestinationCode = '';
        //$scope.searchingCarLength = "";

        var skip = $scope.skip = 0;
        var count = $scope.count = 10;
        //初始化列表
        var loadUsers = function ( skip, count) {
            dataService.GetMyOrderList(skip, count).then(function (res) {
                console.info("loadUsers: getMyOrderList: ", res);
                var list = res.data.Response.Orders;
                for (var i = 0; i < list.length; i++) {
                    list[i].Ids = i + 1 + (skip * count);
                    list[i].IsFocus = (list[i].IsFocus == true) ? '是' : '否';
                    list[i].Amount = list[i].Amount + '' + list[i].AmountUnit;
                }
                $scope.gridApi.grid.options.totalItems = res.data.Response.Total;
                $scope.carrierOrderList = { data: list };
                $scope.height = ((($scope.carrierOrderList.data.length+1) * 30) + 50);
                var currentPage = $scope.gridApi.grid.options.paginationCurrentPage;
                var totalPages = $scope.gridApi.pagination.getTotalPages();
                if (totalPages < currentPage) { $scope.gridApi.grid.options.paginationCurrentPage = 1; };
            }, function (rej) {
                $ngBootbox.alert(rej.response);
            });
        };



        //分页初始化
        var paginationOptions = {
            pageNumber: 0,
            pageSize: 10,
            currentPage: 1,
            sort: null,
            useCache: true
        };
        var _carrierOrderList = $scope.carrierOrderList = {
            data: [],
            enableSorting: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "序号", field: 'Ids', enableColumnMenu: false, },
                { displayName: "Id", field: 'Id', enableColumnMenu: false },
                { displayName: "Contacts", field: 'Contacts', enableColumnMenu: false },
                { displayName: "Category", field: 'Category', enableColumnMenu: false },
                { displayName: "DestinationName", field: 'Line.DestinationName', enableColumnMenu: false },
                { displayName: "StartingPlaceName", field: 'Line.StartingPlaceName', enableColumnMenu: false },
                { displayName: "CarLength", field: 'CarLength', enableColumnMenu: false },
                { displayName: "Telephone", field: 'Telephone', enableColumnMenu: false },
                { displayName: "ContactsChatId", field: 'ContactsChatId', enableColumnMenu: false },
                { displayName: "IsFocus", field: 'IsFocus', enableColumnMenu: false },
                { displayName: "Amount", field: 'Amount', enableColumnMenu: false },
                { displayName: "DeliveryTime", field: 'DeliveryTime', enableColumnMenu: false },
                { displayName: "ArrivalTime", field: 'ArrivalTime', enableColumnMenu: false },
                {
                    name: "操作", field: '操作',
                    cellFilter: 'checkmark',
                    cellTemplate: '<a title="详情" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editOrderDetail(row.entity)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: function (gridApi) {
                $scope.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                    paginationOptions.pageNumber = newPage * pageSize - pageSize;
                    paginationOptions.pageSize = pageSize;
                    paginationOptions.currentPage = newPage;
                    $scope.gridApi.grid.options.paginationCurrentPage = newPage;
                    $scope.skip = paginationOptions.pageNumber;
                    $scope.count = paginationOptions.pageSize;
                    loadUsers(parseInt(newPage - 1), paginationOptions.pageSize);
                });
            }
        };

        loadUsers(paginationOptions.pageNumber, paginationOptions.pageSize);
    }]);

})();